package model.data.database;

import java.sql.Connection;
import java.sql.SQLException;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

public class SqlServerConnector implements DatabaseConnector {
	
	/** Constructs a new SqlServerConnection */
	public SqlServerConnector() {
	}
	
	/** Connects to the sql server database */
	@Override
	public Connection getConnection() throws DatabaseCommunicationException {
		try {
			// Load driver
			this.loadDriver();
			
			// Establish a connection to the database
			// TODO store values in a properties file
			SQLServerDataSource dataSource = new SQLServerDataSource();
			dataSource.setServerName("84.193.85.159");
			dataSource.setPortNumber(1433);
			dataSource.setDatabaseName("CVOShop");
			dataSource.setUser("shopuser");
			dataSource.setPassword("cvo123");
			return dataSource.getConnection();
		}
		catch (ClassNotFoundException cnfe) {
			throw new DatabaseCommunicationException("Could not load driver", cnfe);
		}
		catch (SQLException se) {
			throw new DatabaseCommunicationException("Could not connect", se);
		}
		catch (Exception e) {
			throw new DatabaseCommunicationException(e.getMessage(), e);
		}
	}
	
	/* Loads driver */
	private void loadDriver() throws ClassNotFoundException{
		// Load JDBC driver
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	}
}